﻿namespace DepartmentEditForm
{
    using System;
    using System.Data;

    using Microsoft.SharePoint;

    public class DepartmentViewReferences
    {
        #region Fields

        SPListItemCollection _DepartmentData;
        DataTable _DepartmentDataTable;

        #endregion Fields

        #region Properties

        public string AllFields
        {
            get;
            set;
        }

        public string DepartmentCode
        {
            get;
            set;
        }

        public SPListItemCollection DepartmentData
        {
            get
            {
                return _DepartmentData;
            }
            set
            {
                _DepartmentData = value;
            }
        }

        public DataTable DepartmentDataTable
        {
            get
            {
                if ( _DepartmentDataTable == null )
                    _DepartmentDataTable = new DataTable( );
                return _DepartmentDataTable;
            }
            set
            {
                _DepartmentDataTable = value;
            }
        }

        public string DepartmentName
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        public string GetIDFromProjectID( SPWeb storeWeb, string projectID )
        {
            SPQuery singleDepartmentDepartments = new SPQuery( );

            SPList localDepartmentData = storeWeb.Lists[ DepartmentName ];

            if ( DepartmentName.Equals( "Stores Detail" ) )
            {
                singleDepartmentDepartments.Query = "<Where><Eq><FieldRef Name='Store_x0020_Reference_x0020_ID' /><Value Type='Number'>" + projectID.Trim( ) + "</Value></Eq></Where>";
                singleDepartmentDepartments.ViewFields = "<FieldRef Name='ID'/>";
            }
            else
            {
                singleDepartmentDepartments.Query = "<Where><Eq><FieldRef Name='Store_x0020_Reference_x0020_ID' /><Value Type='Number'>" + projectID.Trim( ) + "</Value></Eq></Where>";
                singleDepartmentDepartments.ViewFields = "<FieldRef Name='ID'/>";
            }

            SPListItemCollection departmentsID = localDepartmentData.GetItems( singleDepartmentDepartments );
            return departmentsID[ 0 ][ "ID" ].ToString( );
        }

        public void PopulateCurrentDataCollection( SPWeb storeWeb )
        {
            PopulateCurrentDataCollection( storeWeb, string.Empty, string.Empty );
        }

        public void PopulateCurrentDataCollection( SPWeb storeWeb, string fieldValue, string projectValue )
        {
            SPQuery singleDepartments = new SPQuery( );
            TargetFieldsDataCollectionBL target = new TargetFieldsDataCollectionBL( );
            SPQuery newData = new SPQuery( );
            SPList localDepartmentData = storeWeb.Lists[ DepartmentName ];

            string currentLocalID = GetIDFromProjectID( storeWeb, projectValue );
            if ( DepartmentName.Equals( "Stores Detail" ) )
            {
                singleDepartments.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Number'>" + currentLocalID + "</Value></Eq></Where>";
                singleDepartments.ViewFields = AllFields;
            }
            else
            {
                singleDepartments.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Number'>" + currentLocalID + "</Value></Eq></Where>";
                singleDepartments.ViewFields = AllFields;
            }

            DepartmentData = localDepartmentData.GetItems( newData );
            DepartmentDataTable = DepartmentData.GetDataTable( );
            DepartmentCode = target.GetDepartmentAbbreviations( storeWeb, DepartmentName );
        }

        #endregion Methods
    }
}